package com.foreveross.mofang.serviceconsole.web.controller.core;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import org.dayatang.querychannel.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.foreveross.mofang.serviceconsole.application.core.ChannelApplication;
import com.foreveross.mofang.serviceconsole.application.dto.ChannelDTO;
import com.foreveross.mofang.serviceconsole.web.controller.common.BaseController;

/**
 * 渠道管理controller
 * 
 * @author fb
 *
 */
@Controller
@RequestMapping("/core/Channel")
public class ChannelController extends BaseController{
		
	@Inject
	private ChannelApplication channelApplication;
	
	@ResponseBody
	@RequestMapping("/add")
	public Map<String, Object> add(ChannelDTO channelDTO) {
		Map<String, Object> result = new HashMap<String, Object>();
		channelApplication.saveChannel(channelDTO, getLogionUser());
		result.put("result", "新渠道创建成功!");
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/update")
	public Map<String, Object> update(ChannelDTO channelDTO) {
		Map<String, Object> result = new HashMap<String, Object>();
		channelApplication.updateChannel(channelDTO, getLogionUser());
		result.put("result", "渠道配制更新成功!");
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping("/pageJson")
	public Page pageJson(ChannelDTO channelDTO, @RequestParam int page, @RequestParam int pagesize) {
		Page<ChannelDTO> all = channelApplication.pageQueryChannel(channelDTO, page, pagesize);
		return all; 
	}
	
	
	@ResponseBody
	@RequestMapping("/migration")
	public Map<String, Object> migration(@RequestParam Long group, @RequestParam("ids[]") Long ids[]) {
		Map<String, Object> result = new HashMap<String, Object>();
		channelApplication.migration(group, ids);
		result.put("result", "渠道迁移成功!");
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Map<String, Object> delete(@RequestParam("ids[]") Long ids[]) {
		Map<String, Object> result = new HashMap<String, Object>();
        channelApplication.removeChannels(ids, getLogionUser());
		result.put("result", "渠道删除成功!");
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/get/{id}")
	public Map<String, Object> get(@PathVariable Long id) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("data", channelApplication.getChannel(id));  
		return result;
	}
}
